package ch.ethz.fcl.metrobuzz.render.accessbility.interaction;

import java.util.List;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.controller.external.ActionEvent;
import ch.ethz.fcl.metrobuzz.data.transit.Stop;
import ch.ethz.fcl.metrobuzz.render.accessbility.AccessibilityRender;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.spatialindex.object3d.Point3D;
import ch.ethz.fcl.metrobuzz.tools.MBTools;

public class AcceMouseAction {
	private MBContainer container;
	private AccessibilityRender accRen;

	private List<Stop> stops;
	private Stop originStop = null;
	private Stop destinationStop = null;

	private Stop lastOriginStop = null;

	public AcceMouseAction(MBContainer container, AccessibilityRender accRen) {
		this.container = container;
		this.accRen = accRen;

		stops = container.getData().getPublicTransit().getStops();
	}

	public void handleActionEvent() {
		ActionEvent action = container.getControl().getTuioListener()
				.getTuioEvent();
		if (action == null)
			action = container.getControl().getMouseListener().getRightMouse();

		Stop selectedStop = null;
		if (action != null) {
			Point3D event = container
					.getViewer()
					.getProjection()
					.getWorldCoord(
							new Point2D(action.getEventX(), action.getEventY()),
							0);
			if (action.getAction() == ActionEvent.ACTION_DOWN) {
				float minDist = Float.MAX_VALUE;

				for (Stop stop : stops) {
					float distance = MBTools.distance(stop.getX(), stop.getY(),
							event.getX(), event.getY());
					if (distance < minDist) {
						minDist = distance;
						selectedStop = stop;
					}
				}
			}
		}

		if (selectedStop != null)
			if (originStop == null) {
				originStop = selectedStop;
				destinationStop = null;
				accRen.getFlowMapView().update();
				accRen.getIsochroneView().update();

				lastOriginStop = originStop;

				AccessibilityRender.ACCESSIBILITY_MAP_VIEW = false;
				AccessibilityRender.ROAD_EFFICIENCY_VIEW = false;
				accRen.getGlobalPanel().getAcceMapCB().setSelected(false);
				accRen.getGlobalPanel().getAcceMapCB().setSelected(false);

				if (!AccessibilityRender.FLOW_MAP_VIEW
						&& !AccessibilityRender.ISOCHRONE_VIEW) {
					AccessibilityRender.FLOW_MAP_VIEW = true;
					AccessibilityRender.ISOCHRONE_VIEW = true;
					accRen.getLocalPanel().getFlowMapCB().setSelected(true);
					accRen.getLocalPanel().getIsochroneCB().setSelected(true);
				}
			} else if (destinationStop == null) {
				destinationStop = selectedStop;

				// accRen.getFlowMapView().debugODPath();
			} else if (selectedStop != lastOriginStop) {
				originStop = selectedStop;
				destinationStop = null;

				lastOriginStop = originStop;
				accRen.getFlowMapView().update();
				accRen.getIsochroneView().update();

				AccessibilityRender.ACCESSIBILITY_MAP_VIEW = false;
				AccessibilityRender.ROAD_EFFICIENCY_VIEW = false;
				accRen.getGlobalPanel().getAcceMapCB().setSelected(false);
				accRen.getGlobalPanel().getAcceMapCB().setSelected(false);

				if (!AccessibilityRender.FLOW_MAP_VIEW
						&& !AccessibilityRender.ISOCHRONE_VIEW) {
					AccessibilityRender.FLOW_MAP_VIEW = true;
					AccessibilityRender.ISOCHRONE_VIEW = true;
					accRen.getLocalPanel().getFlowMapCB().setSelected(true);
					accRen.getLocalPanel().getIsochroneCB().setSelected(true);
				}
			} else {
				destinationStop = null;
			}
	}

	public Stop getOrigin() {
		return originStop;
	}

	public Stop getDestination() {
		return destinationStop;
	}
}
